package trade.axht.springcloud.springcloudconsumermovie.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import trade.axht.springcloud.springcloudconsumermovie.entity.User;

/**
 * 描述:
 * 电影微服务Controller
 *
 * @outhor Administrator
 * @create 2018-04-16 22:54
 */
@RestController
public class MsSimpleConsumerMovieController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    /*@Value("${user.userServicePath}")
    private String userServicePath;

    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id){
        return this.restTemplate.getForObject(this.userServicePath+id,User.class);
    }*/

    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id){
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("springms-simple-provider-user");
        System.out.println(">>>>>" + " " + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());

        return this.restTemplate.getForObject("http://springms-simple-provider-user/simple/" + id, User.class);
    }

    /**
     * 添加给 springms-sidecar 微服务做测试用的代码。
     *
     * @return
     */
    @GetMapping("/sidecar")
    public String sidecar() {
        return this.restTemplate.getForObject("http://springms-sidecar/", String.class);
    }

    /**
     * 添加给 springms-sidecar 微服务做测试用的代码。
     *
     * @return
     */
    @GetMapping("/sidecar/health.json")
    public String sidecarHealth() {
        return this.restTemplate.getForObject("http://springms-sidecar/health.json", String.class);
    }


    @GetMapping("/choose")
    public String test(){
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("springms-simple-provider-user");
        System.out.println("00000" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());

        ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("springms-simple-provider-user2");
        System.out.println("222222222222222222" + ":" + serviceInstance2.getServiceId() + ":" + serviceInstance2.getHost() + ":" + serviceInstance2.getPort());

        return "choose successful";

    }



}
